package com.haighcom.controller;

import com.haighcom.common.QiniuUtils;
import com.haighcom.constant.MessageConstant;
import com.haighcom.entity.PageResult;
import com.haighcom.entity.QueryPageBean;
import com.haighcom.entity.Result;
import com.haighcom.model.Setmeal;
import com.haighcom.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @Autowired
private SetMealService setMealService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile){
    //生成文件名称
        String originalFileName = imgFile.getOriginalFilename();
        //截取文件后缀
        int index = originalFileName.lastIndexOf(".");
        String ex = originalFileName.substring(index - 1);//生成文件后缀
        String Filename = UUID.randomUUID().toString()+ex;//组装文件名

        //使用七牛云上传图片
        try{
            QiniuUtils.upload2Qiniu(imgFile.getBytes(),Filename);
        }catch(Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
        return new Result(true,MessageConstant.PIC_UPLOAD_SUCCESS,Filename);
    }
    @PostMapping("/findPage")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){

        PageResult pageResult = setMealService.queryPage(
                    queryPageBean.getCurrentPage(),
                    queryPageBean.getPageSize(),
                    queryPageBean.getQueryString()
            );

        return pageResult;
    }
}
